package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.Statistics_chargeDto;
import edu.suda.xsap.service.Statistics_chargeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Month;
import java.time.Year;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistics_charge")
@Slf4j
public class Statistics_chargeController {

    @Autowired
    private Statistics_chargeService statistics_chargeService;

    @GetMapping("/year")
    public Result year(@RequestParam("year1")Year year1, @RequestParam("year2")Year year2) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_chargeDto> statistics_chargeDtos = statistics_chargeService.findYEAR(year1, year2);
            //
            return Result.success(statistics_chargeDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @GetMapping("/month")
    public Result month(@RequestParam("year")Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_chargeDto> statistics_chargeDtos =
                    statistics_chargeService.findMONTH(year);
            Map<String, Double> pair = new HashMap<>();
            Integer length = 12;
            for (int i = 0; i < statistics_chargeDtos.size(); i++) {
                Statistics_chargeDto jsonObject = statistics_chargeDtos.get(i);
                pair.put(String.valueOf(jsonObject.getMonth().getValue()), jsonObject.getMoney());
            }
            List<Double> list = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                if (pair.get(String.valueOf(i + 1)) == null) {
                    list.add((double) 0);
                } else {
                    list.add(pair.get(String.valueOf(i + 1)));
                }
            }
            return Result.success(list);

        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @GetMapping("/quarter")
    public Result quarter(@RequestParam("year")Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_chargeDto> statistics_chargeDtos =
                    statistics_chargeService.findQUARTER(year);
            //
            return Result.success(statistics_chargeDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

//    public Result returnMoney(){
//        try {
//            List<HomePageDto> list =
//            homepageService.getMonthlyCharge(Year.of(YearMonth.now().getYear()), YearMonth.now().getMonth());
//            List<Map<String, Integer>> result = new ArrayList<>();
//            Map<String, Integer> pair = new HashMap<>();
//            // 遍历JSON对象数组
//            for (int i = 0; i < list.size(); i++) {
//                System.out.println(list.size());
//                HomePageDto jsonObject = list.get(i);
//                System.out.println(jsonObject);
//
//                pair.put(jsonObject.getDay(), jsonObject.getRechargeMoney());
//            }
//            LocalDate today = LocalDate.now();
//
//            Integer lenth = today.getDayOfMonth();
//            List<Integer> list1 = new ArrayList<>();
//            for (int i = 0; i < lenth; i++) {
//                if (pair.get(String.valueOf(i + 1)) == null) {
//                    list1.add(0);
//                } else {
//                    list1.add(pair.get(String.valueOf(i + 1)));
//                }
//
//            }
//            System.out.println(list1);
//            return Result.success(list1);
//        }catch (Exception e) {
//            log.error("查询失败", e);
//            return Result.failure(e.getMessage(), e);
//        }
//    }
}
